package com.ziyun.erp.modules.goodsImage.service.impl;

import com.utility.New;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.goodsImage.dao.GoodsImageDao;
import com.ziyun.erp.modules.goodsImage.entity.GoodsImageEntity;
import com.ziyun.erp.modules.goodsImage.service.GoodsImageService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsImageService")
public class GoodsImageServiceImpl extends ServiceImpl<GoodsImageDao, GoodsImageEntity> implements GoodsImageService {

    @Autowired
    private GoodsImageDao goodsImageDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<GoodsImageEntity> page = this.selectPage(
                new Query<GoodsImageEntity>(params).getPage(),
                new EntityWrapper<GoodsImageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveImage(GoodsImageEntity goodsImage) {
        Integer integer = goodsImageDao.saveImage(goodsImage);
        return integer;
    }

    /**
     * 搜索图片按id字符串集
     * @param ids
     * @return
     */
    @Override
    public List<GoodsImageEntity> selectImageEntity(String ids) {
        List<Integer> list = changeStr2List(ids);
        List<GoodsImageEntity> goodsList = goodsImageDao.selectImageEntityNew(list);
//        List<GoodsImageEntity> goodsList = goodsImageDao.selectImageEntity(ids);
        return goodsList;
    }

    /**
     *字符串转为Integer数组
     */
    private List<Integer> changeStr2List(String str){
        if(StringUtils.isNotBlank(str)){
            String[] split = str.split(",");
            List<Integer> ids = New.arrayList();
            for(int j = 0;j<split.length;j++){
                if(StringUtils.isNotBlank(split[j])){
                    ids.add(Integer.valueOf(split[j]));
                }
            }
            return ids;
        }else{
            return null;
        }
    }

    /**
     * 更新图片表
     * @param categoryId
     * @param imgId
     */
    @Override
    public void updateImage(Integer categoryId, String imgId) {
        goodsImageDao.updateImage(categoryId,imgId);
    }

    /**
     * 标记删除不需要的图片
     * @param ids
     */
    @Override
    public void flagDelImage(List<Integer> ids) {
        goodsImageDao.fagDelImageByIds(ids);
    }

    @Override
    /**
     * 批量插入图片,返回主键字符串
     */
    @Transactional(rollbackFor = Exception.class)
    public String handlerInsert(List<String> urlList, String type,Long goodsId) {
        StringBuffer iamgeIds = new StringBuffer();
        for(int i= 0;i<urlList.size();i++){
            GoodsImageEntity goodsImageEntity = new GoodsImageEntity();
            //对urlList.get(i)做一些处理
            //http://image.11ziyun.com/public/images/b0/38/05/89213ab14a7f8ed0088d7ed8896ad2ce105ef1cd.jpg?1471833739#w
            //http://images.11ziyun.com/image/20180309/20180309154655_886.jpg
            if(urlList.get(i).indexOf("http://image.11ziyun.com/") != -1){
                String substring = urlList.get(i).replaceFirst("http://image.11ziyun.com/","");
                goodsImageEntity.setImageUrl(substring);
            }else if(urlList.get(i).indexOf("http://images.11ziyun.com/") != -1){
                String substring = urlList.get(i).replaceFirst("http://images.11ziyun.com/","");
                goodsImageEntity.setImageUrl(substring);
            }else{
                goodsImageEntity.setImageUrl(urlList.get(i));
            }

            goodsImageEntity.setDisable(false);
            goodsImageEntity.setType(type.equals("image_url")?0:1);
            goodsImageEntity.setRelationId(goodsId);
            goodsImageEntity.setCreateName("dataMigration");
            this.baseMapper.insert(goodsImageEntity);
            iamgeIds.append(goodsImageEntity.getId());
            iamgeIds.append(",");
        }
        return iamgeIds.toString();
    }
    @Override
    public void newImageUpdate(Long goodsId,List<Integer> ids){
        this.baseMapper.newImageUpdate(goodsId,ids);

    }



}
